package com.flowable.service.impl;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.flowable.common.result.CommonResult;
import com.flowable.service.HistoricService;
import org.flowable.engine.HistoryService;
import org.flowable.engine.history.HistoricActivityInstance;
import org.flowable.engine.history.HistoricProcessInstance;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.text.SimpleDateFormat;
import java.util.List;


/**
 * 历史 实现
 */
@Service
public class HistoricServiceImpl implements HistoricService {

    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

    @Autowired
    private HistoryService historyService;

    /**
     * 查询 历史流程实例
     *
     * @param processInstanceId 流程实例ID
     * @return
     */
    @Override
    public CommonResult historicProcessInstanceQuery(String processInstanceId) {
        HistoricProcessInstance historicProcessInstance = historyService.createHistoricProcessInstanceQuery()
                .processInstanceId(processInstanceId)
                .finished() // 已完成状态
                .orderByProcessInstanceStartTime().asc()
                .singleResult();
        JSONObject jsonObject = new JSONObject();
        if (historicProcessInstance == null) {
            return CommonResult.failed("historicProcessInstance is null");
        }
        // 流程实例ID
        jsonObject.put("hisProcInstId", historicProcessInstance.getId());
        // 流程定义ID
        jsonObject.put("hisProcInstProcDefId", historicProcessInstance.getProcessDefinitionId());
        // 流程开始时间
        jsonObject.put("hisProcInstStartTime", sdf.format(historicProcessInstance.getStartTime()));
        // 流程结束时间
        jsonObject.put("hisProcInstEndTime", sdf.format(historicProcessInstance.getEndTime()));
        // 流程花费时间
        jsonObject.put("hisProcInstDurationInMillis", historicProcessInstance.getDurationInMillis());
        return CommonResult.success(jsonObject);
    }

    /**
     * 查询 历史活动实例
     *
     * @param processInstanceId 流程实例ID
     * @return
     */
    @Override
    public CommonResult historicActivityInstanceQuery(String processInstanceId) {
        List<HistoricActivityInstance> list = historyService.createHistoricActivityInstanceQuery()
                .processInstanceId(processInstanceId)
                .finished() // 已完成状态
                .orderByHistoricActivityInstanceStartTime().asc()
                .list();
        JSONObject jsonObject = new JSONObject();
        if (CollectionUtils.isEmpty(list)) {
            return CommonResult.failed("HistoricActivityInstanceList is empty");
        }
        JSONArray jsonArray = new JSONArray();
        jsonObject.put("data", jsonArray);
        for (HistoricActivityInstance hisActInst : list) {
            JSONObject tmpJsonObject = new JSONObject();
            // 历史活动ID
            tmpJsonObject.put("hisActInstId", hisActInst.getId());
            // 流程定义ID
            tmpJsonObject.put("hisActInstIdProcDefId", hisActInst.getProcessDefinitionId());
            // 流程开始时间
            tmpJsonObject.put("hisActInstIdStartTime", sdf.format(hisActInst.getStartTime()));
            // 流程结束时间
            tmpJsonObject.put("hisActInstIdEndTime", sdf.format(hisActInst.getStartTime()));
            // 流程花费时间
            tmpJsonObject.put("hisActInstIdDurationInMillis", hisActInst.getDurationInMillis());
            // 流程处理人
            tmpJsonObject.put("hisActInstIdAssignee", hisActInst.getAssignee());
            // 活动ID
            tmpJsonObject.put("hisActInstIdActId", hisActInst.getActivityId());
            // 活动名称
            tmpJsonObject.put("hisActInstIdActName", hisActInst.getActivityName());
            // 活动类型
            tmpJsonObject.put("hisActInstIdActType", hisActInst.getActivityType());
            jsonArray.add(tmpJsonObject);
        }
        return CommonResult.success(jsonObject);
    }
}
